<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaLogisticsServiceController extends OaBaseController
{
    var $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_logistics_service');
        parent::_initialize();

//        $controller = strtolower(CONTROLLER_NAME);

//        $res ['title'] = '后勤中心';
//        $res ['url'] = addons_url('OaSystem://OaLogisticsService/foodlists', array('mdm' => I('mdm')));
//        $res ['class'] = $controller == 'OaLogisticsService' ? 'current' : '';
//        $nav [] = $res;
//        $this->assign('nav', $nav);
//
//        $res ['title'] = '食堂菜品';
//        $sub_nav[] = $res;
//        $res ['title'] = '食堂订单管理';
//        $res ['url'] = addons_url('OaSystem://OaLogisticsService/ordermanage/type/1', array('mdm' => I('mdm')));
//        $res ['class'] = $controller == 'OaLogisticsService' ? 'current' : '';
//        $sub_nav[] = $res;
//        $res ['title'] = '小卖部商品';
//        $res ['url'] = addons_url('OaSystem://OaLogisticsService/goodslists', array('mdm' => I('mdm')));
//        $res ['class'] = $controller == 'OaLogisticsService' ? 'current' : '';
//        $sub_nav[] = $res;
//
//        $res ['title'] = '票务';
//        $res ['url'] = addons_url('OaSystem://OaLogisticsService/lists/type/3', array('mdm' => I('mdm')));
//        $res ['class'] = $controller == 'OaLogisticsService' ? 'current' : '';
//        $sub_nav[] = $res;
//
//        $res ['title'] = '我的订单';
//        $res ['url'] = addons_url('OaSystem://OaServiceOrder/lists', array('mdm' => I('mdm')));
//        $res ['class'] = $controller == 'OaServiceOrder' ? 'current' : '';
//        $sub_nav[] = $res;
//
//        $this->assign('sub_nav', $sub_nav);
    }

    public function jump() {
        if(!empty($this->first_sub_nav_url))
            redirect($this->first_sub_nav_url);
    }

    function foodlists() {
        for($d = 1; $d <= 6; $d++) {
            $daymenu = db_select('oa_day_foodmenu', ['dayweek'=>$d]);
            foreach ($daymenu as $i => $v) {
                $service = db_find('oa_logistics_service', ['id'=>$v['serviceid']]);
                if(!empty($service['name']))
                    $foodmenu[$i]['day'.strval($d)] = $service['name'].'-'.$service['price'].'元';
            }
        }
        $this->assign('foodmenu', $foodmenu);
        $this->display();
    }

    function goodslists() {
        $res['title'] = '商品excel导入';
        $res['is_buttion'] = false;
        $res['url'] = addons_url ( 'OaSystem://OaLogisticsService/goodsimport' ,array('mdm'=>I('mdm')));
        $top_more_button[] = $res;
        $res['title'] = 'excel导出';
        $res['is_buttion'] = false;
        $res['url'] = addons_url ( 'OaSystem://OaLogisticsService/goodsemport' ,array('mdm'=>I('mdm')));
        $top_more_button[] = $res;
        $this->assign('top_more_button',$top_more_button);
        $this->assign('add_button',true);

        $model = $this->getModel('oa_logistics_service');
        $list_data = $this->_get_model_list($model);
        $grids = $list_data['list_grids'];
        $data = db_select('oa_logistics_service', ['type'=>2]);
        foreach ($data as $i => $v) {
            $editUrl = U('edit', array('id'=> $v['id']));
            $delUrl = U('del', array('id'=> $v['id']));
            $data[$i]['urls'] = "<a href=$editUrl>编辑</a>".'   '."<a class='confirm' href=$delUrl>删除</a>";
        }

        $this->assign('list_grids', $grids);
        $this->assign('list_data', $data);
        $this->display('lists');
    }

    function add()
    {
        if(IS_POST) {
            if($_POST['button_cancel']==1) {
                myprint('button_cancel');
                redirect(U('goodslists'));
            }
            if(empty($_POST['name']) || empty($_POST['brand']) || empty($_POST['price'])) {
                $this->error('请输入商品，品牌或价格!');
                return false;
            }
            if(empty(floatval($_POST['price']))) {
                $this->error('价格请输入正确格式!');
                return false;
            }

            $data['appid'] = $this->appId;
            $data['type'] = 2;
            $data['name'] = $_POST['name'];
            $data['brand'] = $_POST['brand'];
            $data['description'] = $_POST['description'];
            if(($goods = db_find('oa_logistics_service', $data)) != NULL) {
                $data['price'] = floatval($_POST['price']);
                db_save('oa_logistics_service', ['id'=>$goods['id']], $data);
            } else {
                $data['price'] = floatval($_POST['price']);
                if(M('oa_logistics_service')->add($data) == false) {
                    $this->error(__METHOD__.'['.__LINE__.']'.'添加oa_logistics_service失败！');
                    return false;
                }
            }
            $this->success ( '新增' . $model ['title'] . '成功！', U ( 'goodslists?model=' . $model ['name'], $this->get_param ));
        } else {
            $this->display();
        }
    }

    function edit()
    {
        if(IS_POST) {
            if($_POST['button_cancel']==1) {
                $url = addons_url('OaSystem://OaLogisticsService/goodslists', array('mdm' => I('mdm'), 'type'=>$application['type'], 'subtype'=>$application['subtype'], 'application_id'=>$data['application_id']));
                redirect($url);
            }
            if(empty($_POST['name']) || empty($_POST['brand']) || empty($_POST['price'])) {
                $this->error('请输入商品，品牌或价格!');
                return false;
            }
            if(empty(floatval($_POST['price']))) {
                $this->error('价格请输入正确格式!');
                return false;
            }

            $id = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['brand'] = $_POST['brand'];
            $data['description'] = $_POST['description'];
            $data['price'] = floatval($_POST['price']);
            if(db_save('oa_logistics_service', ['id'=>$id], $data) === false) {
                $this->error(__METHOD__.'['.__LINE__.']'.'保存oa_logistics_service失败！');
                return false;
            }
            $this->success ( '修改' . $model ['title'] . '成功！', U ( 'goodslists?model=' . $model ['name'], $this->get_param ));
        } else {
            $id = $_GET['id'];
            $data = db_find('oa_logistics_service', ['id'=>$id]);
            $this->assign('data', $data);
            $this->display();
        }
    }

    function foodOrderManage() {

        if(IS_POST) {
            $status = $_POST['status'];
            redirect(U('foodOrderManage', array('status'=>$status)));
        }

        $type = 1;
        $status = empty(I('status')) ? 0 : I('status');
        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;

        $map['type'] = $type;
        $map['is_del'] = 0;
        $map['status'] = $status;
        $order = ($status==0) ? 'time asc' : 'time desc';
        $len = M('oa_service_order')->where($map)->limit(100)->count();
        $data = M('oa_service_order')->where($map)->order($order)->limit(100)->page($p,$row)->select();
        $status_str = array('预订中', '拒绝', '订单已确认', '用餐未到','订单完成');
        foreach ($data as $i => $v) {
            $data[$i]['status'] = $status_str[$v['status']];
            $data[$i]['date'] = date('Y/m/d', $v['date']);
            $service = db_find('oa_logistics_service', ['id'=>$v['product']]);
            $data[$i]['product'] = $service['name'];

            $confirmUrl = "javascript:if(confirm('确认接受该订单?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>2))."'";
            $rejectUrl = "javascript:refusePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>1))."')";
            $notcomeUrl = "javascript:if(confirm('确定员工没来用餐?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>3))."'";
            $doneUrl = "javascript:if(confirm('确定预订完成?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>4))."'";
            $chaneCommentUrl = "javascript:refusePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>1))."','".$data[$i]['comment']."')";
            if($status == 0) {
                $data[$i]['action'] = "<a href=$confirmUrl><font style='font-weight: bold' size='3'>确认</font></a>".'&nbsp&nbsp'."<a href=$rejectUrl><font style='font-weight: bold' size='3' color='red'>拒绝</font></a>";;
            } else if($status == 1) {
                $data[$i]['action'] = "<a href=$chaneCommentUrl><font style='font-weight: bold' size='3'>修改备注</font></a>";
            } else if($status == 2) {
                // $data[$i]['action'] = "<a href=$doneUrl><font style='font-weight: bold' size='3'>完成</font></a>".'&nbsp&nbsp'."<a class='confirm' href=$notcomeUrl><font style='font-weight: bold' size='3' color='red'>未到</font></a>";
                $data[$i]['action'] = "<a href=$doneUrl><font style='font-weight: bold' size='3'>完成</font></a>".'&nbsp&nbsp'."<a class='confirm' href=$notcomeUrl><font style='font-weight: bold' size='3' color='red'>用餐未到</font></a>";
            }
        }

        foreach ($status_str as $i => $v) {
            $status_sel[$i]['id'] = $i;
            $status_sel[$i]['name'] = $v;
        }
        $status_sel[$status]['selected'] = 'selected';

        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }
        $this->assign('table_name',array('订单号','姓名','菜品','数量','金额','备注','日期','状态','操作'));
        $this->assign('_page',$list_data['_page']);
        $this->assign('status_sel', $status_sel);
        $this->assign('table_data', $data);
        $this->assign('type', $type);
        $this->assign('post_url', U('foodOrderManage'));
        $this->display('ordermanage');
    }

    function goodsOrderManage() {

        if(IS_POST) {
            $status = $_POST['status'];
            redirect(U('goodsOrderManage', array('status'=>$status)));
        }

        $type = 2;
        $status = empty(I('status')) ? 0 : I('status');
        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;

        $map['type'] = $type;
        $map['is_del'] = 0;
        $map['status'] = $status;
        $order = ($status==0) ? 'time asc' : 'time desc';
        $len = M('oa_service_order')->where($map)->limit(100)->count();
        $data = M('oa_service_order')->where($map)->order($order)->limit(100)->page($p,$row)->select();
        // $status_str = array('预订中', '拒绝', '订单已确认', '商品未领');
        $status_str = array('预订中', '拒绝', '订单已确认');
        foreach ($data as $i => $v) {
            $data[$i]['status'] = $status_str[$v['status']];
            $data[$i]['date'] = date('Y/m/d', $v['date']);
            $service = db_find('oa_logistics_service', ['id'=>$v['product']]);
            $data[$i]['product'] = $service['brand'].' '.$service['name'].' '.$service['description'];

            $confirmUrl = "javascript:if(confirm('确认接受该订单?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>2))."'";
            $rejectUrl = "javascript:refusePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>1))."')";
            $notcomeUrl = "javascript:if(confirm('确定员工没来领商品?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>3))."'";
            $doneUrl = "javascript:if(confirm('确定预订完成?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>4))."'";
            $chaneCommentUrl = "javascript:refusePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>1))."','".$data[$i]['comment']."')";

            if($status == 0) {
                $data[$i]['action'] = "<a href=$confirmUrl><font style='font-weight: bold' size='3'>确认</font></a>".'&nbsp&nbsp'."<a href=$rejectUrl><font style='font-weight: bold' size='3' color='red'>拒绝</font></a>";;
            }else if($status == 1) {
                $data[$i]['action'] = "<a href=$chaneCommentUrl><font style='font-weight: bold' size='3'>修改备注</font></a>";
            } else if($status == 2) {
                // $data[$i]['action'] = "<a href=$doneUrl><font style='font-weight: bold' size='3'>完成</font></a>".'&nbsp&nbsp'."<a class='confirm' href=$notcomeUrl><font style='font-weight: bold' size='3' color='red'>未到</font></a>";
                // $data[$i]['action'] = "<a class='confirm' href=$notcomeUrl><font style='font-weight: bold' size='3' color='red'>商品未领</font></a>";
                $data[$i]['action'] = "<a href=$doneUrl><font style='font-weight: bold' size='3'>完成</font></a>";
            }
        }

        foreach ($status_str as $i => $v) {
            $status_sel[$i]['id'] = $i;
            $status_sel[$i]['name'] = $v;
        }
        $status_sel[$status]['selected'] = 'selected';

        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }
        // $this->assign('table_name',array('订单号','姓名','商品','数量','金额','备注','日期','状态','操作'));
        $this->assign('table_name',array('订单号','姓名','商品','数量','金额','备注','创建日期','状态','操作'));
        $this->assign('_page',$list_data['_page']);
        $this->assign('status_sel', $status_sel);
        $this->assign('table_data', $data);
        $this->assign('type', $type);
        $this->assign('post_url', U('goodsOrderManage'));
        $this->display('grocery_ordermanage');
    }

    function changeOrderStatus() {
        $id = $_GET['id'];
        $status = $_GET['status'];
        $type = $_GET['type'];

        if(empty($id) || empty($status) || empty($type)) {
            $this->error(__METHOD__.'['.__LINE__.']'.'参数错误！');
            return false;
        }
        if(($order = db_find('oa_service_order', ['id'=>$id])) == NULL) {
            $this->error(__METHOD__.'['.__LINE__.']'.'找不到对应的订单！');
            return false;
        }
        if (!empty($_GET['comment'])) {
            $order['comment'] = $_GET['comment'];
        }
        $oldstatus = $order['status'];
        $order['status'] = $status;
        if(db_save('oa_service_order', ['id'=>$id], $order) === false) {
            $this->error(__METHOD__.'['.__LINE__.']'.'订单状态修改失败！');
            return false;
        }
        if($type == 1)
        {
            if($status == 2) {
                // 2: 预定被接受
                $content = D('OaOrderServiceView')->where(array('id'=>$id))->find();
                D('OaEmpMsg')->sendLogisticServiceMsg($content['emp_id'],$content,true);
            }
            redirect(U('foodOrderManage', array('status'=>$oldstatus)));
        }else if($type == 2){
            if($status == 2) {
                // 2: 预定被接受
                $content = D('OaOrderServiceView')->where(array('id'=>$id))->find();
                // var_dump($content);
                D('OaEmpMsg')->sendLogisticServiceMsg($content['emp_id'],$content,true);
            }
            redirect(U('goodsOrderManage', array('status'=>$oldstatus)));
        }else if($type == 3){
            redirect(U('ticketOrderManage', array('status'=>$oldstatus)));
        }

    }

    private function serviceOderlist($querypara) {
        $map['status'] = 0;  //  预定中
        $map['type'] = $querypara['type'];
        $s_time = strtotime($querypara['start_date']." 00:00:00");
        $e_time = strtotime($querypara['end_date']." 23:59:59");
        $map['date'] = array('between',array($s_time,$e_time));
        $map['is_del'] = 0;

        $items = D('OaOrderServiceView')->where($map)->select();
        // $items = M('OaServiceOrder')->where($map)->select();

        $data = array();
        foreach ($items as $item){
            $tmp = array();
            $tmp[] = $item['order_no'];
            $tmp[] = $item['emp_name'];
            if ($querypara['type'] == 2){
                $tmp[] = $item['brand'].' '.$item['name'].' '.$item['description'];
            }else {
                $tmp[] = $item['name'];
            }
            if($querypara['type'] != 3){
                $tmp[] = $item['num'];
            }
            $tmp[] = $item['total'];
            $tmp[] = date('Y/m/d', $item['date']);
            array_push($data,$tmp);
        }
        switch ($querypara['type']) {
            case 1:
                $this->assign('table_name',array('订单号','姓名','菜品','数量','金额','预定日期'));
                break;
            case 2:
                $this->assign('table_name',array('订单号','姓名','商品','数量','金额','预定日期'));
                break;
            case 3:
                $this->assign('table_name',array('订单号','姓名','票务类型','金额','预定日期'));
                break;
        }
        // $this->assign('table_name',array('订单号','姓名','商品','数量','金额','时间'));
        $this->assign('table_data', $data);
    }

    private function serviceOderDonelist ($querypara) {
        $food_map['type'] = $querypara['type'];
        $products = M('OaLogisticsService')->where($food_map)->select();
        $data = array();
        $i = 0;
        $tmp = array();
        foreach ($products as $product) {
            $i++;
            $map['product'] = $product['id'];
            $map['is_del'] = 0;
            $map['status'] = 4;  // 完成
            $s_time = strtotime($querypara['start_date']." 00:00:00");
            $e_time = strtotime($querypara['end_date']." 23:59:59");
            $map['time'] = array('between',array($s_time,$e_time));
            $map['is_del'] = 0;
            $items = D('OaLogisticsServiceOrdersView')->where($map)->select();
            if ($querypara['type'] == 2) {
                $itemTmp['style'] = 'width: 180px';
                $itemTmp['data'] = $product['brand'].' '.$product['name'].' '.$product['description'];
                $tmp[] = $itemTmp;
            }else {
                $itemTmp['style'] = 'width: 180px';
                $itemTmp['data'] = $product['name'];
                $tmp[] = $itemTmp;
            }

            $tmp[] = $product['price'];
            $count = 0;
            $total = 0.0;
            foreach ($items as $item) {
                $count = $count + $item['num'];
                $total = $total + $item['total'];
            }
            $tmp[] = $count;
            $tmp[] = $total;
            if( ($i%2) === 0) {
                array_push($data,$tmp);
                $tmp = array();
            }else {
                $tmp[] = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
            }

        }
        switch ($querypara['type']) {
            case 1:
                $this->assign('table_name', array('菜品','当前单价','总数量','总金额','&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp','菜品','当前单价','总数量','总金额'));
                break;
            case 2:
                $this->assign('table_name', array('商品','当前单价','总数量','总金额','&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp','商品','当前单价','总数量','总金额'));
                break;
            case 3:
                $this->assign('table_name', array('票务类型','当前单价','总数量','总金额','&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp','票务类型','当前单价','总数量','总金额'));
                break;
        }
        $this->assign('table_data', $data);
    }

    private function serviceOderConformlist ($querypara) {
        $food_map['type'] = $querypara['type'];
        $products = M('OaLogisticsService')->where($food_map)->select();
        $data = array();
        $i = 0;
        $tmp = array();
        foreach ($products as $product) {
            $i++;
            $map['product'] = $product['id'];
            $map['is_del'] = 0;
            $map['status'] = 2;  // 完成
            $s_time = strtotime($querypara['start_date']." 00:00:00");
            $e_time = strtotime($querypara['end_date']." 23:59:59");
            $map['time'] = array('between',array($s_time,$e_time));
            $map['is_del'] = 0;
            $items = D('OaLogisticsServiceOrdersView')->where($map)->select();
            if ($querypara['type'] == 2) {
                $tmp[] = $product['brand'].' '.$product['name'].' '.$product['description'];;
            }else {
                $tmp[] = $product['name'];
            }
            $tmp[] = $product['price'];
            $count = 0;
            $total = 0.0;
            foreach ($items as $item) {
                $count = $count + $item['num'];
                $total = $total + $item['total'];
            }
            $tmp[] = $count;
            $tmp[] = $total;
            if( ($i%2) === 0) {
                array_push($data,$tmp);
                $tmp = array();
            }else {
                $tmp[] = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
            }

        }
        switch ($querypara['type']) {
            case 1:
                $this->assign('table_name', array('菜品','当前单价','总数量','总金额','&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp','菜品','当前单价','总数量','总金额'));
                break;
            case 2:
                $this->assign('table_name', array('商品','当前单价','总数量','总金额','&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp','商品','当前单价','总数量','总金额'));
                break;
            case 3:
                $this->assign('table_name', array('票务类型','当前单价','总数量','总金额','&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp','票务类型','当前单价','总数量','总金额'));
                break;
        }
        $this->assign('table_data', $data);
    }

    private function serviceOderNotDonelist ($querypara) {
        $map['status'] = 3;  //  订餐未到
        $map['type'] = $querypara['type'];
        $s_time = strtotime($querypara['start_date']." 00:00:00");
        $e_time = strtotime($querypara['end_date']." 23:59:59");
        $map['date'] = array('between',array($s_time,$e_time));
        $map['is_del'] = 0;

        $items = D('OaOrderServiceView')->where($map)->select();
        $data = array();
        foreach ($items as $item){
            $tmp = array();
            $tmp[] = $item['order_no'];
            $tmp[] = $item['emp_name'];
            $tmp[] = $item['brand'].' '.$item['name'].' '.$item['description'];
            $tmp[] = $item['num'];
            $tmp[] = $item['total'];
            $tmp[] = date('Y/m/d', $item['date']);
            array_push($data,$tmp);
        }
        switch ($querypara['type']) {
            case 1:
                $this->assign('table_name',array('订单号','姓名','菜品','数量','金额','预定日期'));
                break;
            case 2:
                $this->assign('table_name',array('订单号','姓名','商品','数量','金额','订单创建日期'));
                break;
        }

        $this->assign('table_data', $data);
    }
    function foodStatistics() {
        if(IS_POST) {
            $querypara = json_encode($_POST);
            var_dump($querypara);
            redirect(U('foodStatistics', array('query'=>$querypara)));
        } else {
            $querypara = json_decode($_GET['query'], true);

            if(empty($querypara['start_date']) || empty($querypara['end_date'])) {
                $querypara['start_date'] = date("Y-m-d");
                $querypara['end_date'] = date("Y-m-d");
                $querypara['status'] = 0;
            }
            $querypara['type'] = 1;

            switch ($querypara['status']) {
                case 0 :
                    $this->serviceOderlist($querypara);
                    break;
                case 1:
                    $this->serviceOderConformlist($querypara);
                    break;
                case 3:
                    $this->serviceOderNotDonelist($querypara);
                    break;
                case 4:
                    $this->serviceOderDonelist($querypara);
                    break;
            }

            $status = $querypara['status'];
            $status_sel = array(
                array('id'=>0,'name'=>'订餐统计'),
                array('id'=>3,'name'=>'订餐未到统计'),
                array('id'=>4,'name'=>'菜品销售统计')
            );
            foreach ($status_sel as $i => $v) {
                if ($status_sel[$i]['id'] == $status) {
                    $status_sel[$i]['selected'] = 'selected';
                }
            }

            $this->assign('data',$querypara);
            $this->assign('status_sel', $status_sel);
            $this->assign('query', $query);
            $this->assign('post_url', U('foodStatistics'));
            $this->display('orderstatistics');
        }

    }

    function goodsStatistics() {
        if(IS_POST) {
            $querypara = json_encode($_POST);
            var_dump($querypara);
            redirect(U('goodsStatistics', array('query'=>$querypara)));
        } else {
            $querypara = json_decode($_GET['query'], true);

            if (empty($querypara['start_date']) || empty($querypara['end_date'])) {
                $querypara['start_date'] = date("Y-m-d");
                $querypara['end_date'] = date("Y-m-d");
                $querypara['status'] = 0;
            }
            $querypara['type'] = 2;

            switch ($querypara['status']) {
                case 0 :
                    $this->serviceOderlist($querypara);
                    break;
                case 1:
                    $this->serviceOderConformlist($querypara);
                    break;
                case 2:
                    $this->serviceOderNotDonelist($querypara);
                    break;
                case 4:
                    $this->serviceOderDonelist($querypara);
                    break;
            }

            // $status_str = array('预定商品统计', '商品销售统计')
            $status = $querypara['status'];
            $status_sel = array(
                array('id'=>0,'name'=>'预定商品统计'),
                array('id'=>4,'name'=>'商品销售统计')
            );
            foreach ($status_sel as $i => $v) {
                if ($status_sel[$i]['id'] == $status) {
                    $status_sel[$i]['selected'] = 'selected';
                }
            }

            $this->assign('data', $querypara);
            $this->assign('status_sel', $status_sel);
            $this->assign('query', $query);
            $this->assign('post_url', U('goodsStatistics'));
            $this->display('orderstatistics');
        }
    }

    function foodimport() {
        if (IS_POST) {
            $data['file'] = I('post.foodmenu');
            $data['valid_date'] = date('y-m-d h:i:s', time());

            if (!intval($data['file'])) $this->error("数据文件未上传！");

            if ($this->import_foodmenu_data_from_excel($data['file'], $data['valid_date'], $data['comment'])) {
                $this->success('保存成功！', U('foodlists'/*'import?model=' . $this->model ['name'], $this->get_param */), 600);
            }
        } else {
            $res['title'] = '本周每日菜品表';
            $res['is_must'] = true;
            $res['is_show'] = 1;
            $res['name'] = 'foodmenu';
            $res['type'] = 'file';
            $fields[] = $res;
            $post_url = addons_url('OaSystem://OaLogisticsService/foodimport');
            $this->assign('fields', $fields);
            $this->assign('post_url', $post_url);
            $this->assign('import_template', 'template_foodmenu.xlsx');
            $this->display('import');
        }
    }

    function goodsimport() {
        if (IS_POST) {
            $data['file'] = I('post.goods');
            $data['valid_date'] = date('y-m-d h:i:s', time());

            if (!intval($data['file'])) $this->error("数据文件未上传！");

            if ($this->import_goods_data_from_excel($data['file'], $data['valid_date'], $data['comment'])) {
                $this->success('保存成功！', U('goodslists'/*'import?model=' . $this->model ['name'], $this->get_param */), 600);
            }
        } else {
            $res['title'] = '小卖部商品列表';
            $res['is_must'] = true;
            $res['is_show'] = 1;
            $res['name'] = 'goods';
            $res['type'] = 'file';
            $fields[] = $res;
            $post_url = addons_url('OaSystem://OaLogisticsService/goodsimport');
            $this->assign('fields', $fields);
            $this->assign('post_url', $post_url);
            $this->assign('import_template', 'template_goods.xlsx');
            $this->display('import');
        }
    }

    function goodsemport() {
        $goods = db_select('oa_logistics_service', ['type'=>2]);
        $data = array();
        $headArr = array("商品", "品牌", "价格", "描述");
        $data[] = $headArr;
        foreach ($goods as $v){
            $tmp = array();
            $tmp[] = $v['name'];
            $tmp[] = $v['brand'];
            $tmp[] = $v['price'];
            $tmp[] = $v['description'];
            $data[] = $tmp;
        }
        $filename = "小卖部商品列表";
        outExcel($data,$filename);
    }

    function foodemport() {
        $data = array();
        $headArr = array("星期一", "价格", "星期二", "价格", "星期三", "价格", "星期四", "价格", "星期五", "价格", "星期六", "价格");
        $data[] = $headArr;
        for($d = 1; $d <= 6; $d++) {
            $dayFoods = db_select('oa_day_foodmenu', ['dayweek'=>$d]);
            foreach ($dayFoods as $i => $v) {
                $food = db_find('oa_logistics_service', ['id'=>$v['serviceid']]);
                $data[$i+1][($d-1)*2] = $food['name'];
                $data[$i+1][($d-1)*2+1] = $food['price'];
            }
        }
        $filename = "每周菜品列表";
        outExcel($data, $filename);
    }

    private function import_foodmenu_data_from_excel($file_id, $date = NULL, $comment = NULL) {
        if ($date == NULL) return false;

        $column = array (
            'A'=>'food1',
            'B'=>'price1',
            'C'=>'food2',
            'D'=>'price2',
            'E'=>'food3',
            'F'=>'price3',
            'G'=>'food4',
            'H'=>'price4',
            'I'=>'food5',
            'J'=>'price5',
            'K'=>'food6',
            'L'=>'price6',
        );

        $excel = importFormExcel($file_id, $column);
        if ($excel['status']) {
            $map['dayweek'] = array('neq', 10);
            M('oa_day_foodmenu')->where($map)->delete();
            foreach  ($excel['data'] as $row) {
                for($i = 1; $i <= 6; $i++) {
                    $foodname = $row['food'.strval($i)];
                    $price = $row['price'.strval($i)];
                    if(empty($foodname))
                        continue;
                    $data = array();
                    $data['appid'] = $this->appId;
                    $data['type'] = 1;
                    $data['name'] = $foodname;
                    $data['price'] = $price;
                    if(($food = db_find('oa_logistics_service', ['type'=>1,'name'=>$foodname])) != NULL) {
                        $foodid = $food['id'];
                        db_save('oa_logistics_service', ['id'=>$foodid], $data);
                    }
                    else {
                        $foodid = M('oa_logistics_service')->add($data);
                        if($foodid == false) {
                            $this->error(__METHOD__.'['.__LINE__.']'.'添加oa_logistics_service失败！');
                            return false;
                        }
                    }
                    $data2['serviceid'] = $foodid;
                    $data2['dayweek'] = $i;
                    M('oa_day_foodmenu')->add($data2);
                }
            }
            return true;
        } else {
            $this->error('请检查文件格式');
            return false;
        }
    }

    private function import_goods_data_from_excel($file_id, $date = NULL, $comment = NULL) {
        if ($date == NULL) return false;
        $column = array (
            'A'=>'name',
            'B'=>'brand',
            'C'=>'price',
            'D'=>'description',
        );

        $excel = importFormExcel($file_id, $column);
        if ($excel['status']) {
            foreach  ($excel['data'] as $row) {
                $data = array();
                $data['appid'] = $this->appId;
                $data['type'] = 2;
                $data['name'] = $row['name'];
                $data['brand'] = $row['brand'];
                $data['description'] = $row['description'];
                if(($goods = db_find('oa_logistics_service', $data)) != NULL) {
                    $data['price'] = $row['price'];
                    db_save('oa_logistics_service', ['id'=>$goods['id']], $data);
                } else {
                    $data['price'] = $row['price'];
                    if(M('oa_logistics_service')->add($data) == false) {
                        $this->error(__METHOD__.'['.__LINE__.']'.'添加oa_logistics_service失败！');
                        return false;
                    }
                }
            }
            return true;
        } else {
            $this->error('请检查文件格式');
            return false;
        }
    }
    function ticketOrderManage() {

        if(IS_POST) {
            $status = $_POST['status'];
            redirect(U('ticketOrderManage', array('status'=>$status)));
        }

        $type = 3;
        $status = empty(I('status')) ? 0 : I('status');
        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;

        $map['type'] = $type;
        $map['is_del'] = 0;
        if ($status == 0) {
            $map['status'] = array('in',array($status,5));
        }else {
            $map['status'] = $status;
        }

        $order = ($status==0) ? 'time asc' : 'time desc';
        $len = M('oa_service_order')->where($map)->limit(100)->count();
        $data = M('oa_service_order')->where($map)->order($order)->limit(100)->page($p,$row)->select();
        /*
        $status_str = array(
            array('id'=>0,'name'=>'预订中'),array('id'=>1,'name'=>'拒绝'), array('id'=>2,'name'=>'订单已确认'),
            array('id'=>3,'name'=>'商品未领'), array('id'=>4,'name'=>'预订完成'), array('id'=>5,'name'=>'订单取消'),
            array('id'=>6,'name'=>'订单取消确认'));
        */
        $status_str = array('预订中','拒绝','订单已确认','商品未领','预订完成','订单取消','订单取消确认');
        $status_sel = array(
            array('id'=>0,'name'=>'预订中'),array('id'=>1,'name'=>'拒绝'), array('id'=>2,'name'=>'订单已确认'),
            array('id'=>4,'name'=>'预订完成'),array('id'=>6,'name'=>'订单取消确认'));

        foreach ($data as $i => $v) {
            $data[$i]['status'] = $status_str[$v['status']];
            $data[$i]['date'] = date('Y/m/d', $v['date']);
            $service = db_find('oa_logistics_service', ['id'=>$v['product']]);
            $data[$i]['product'] = $service['name'];
            $data[$i]['detials'] .= '身份证号：'.$data[$i]['idnum'].'<br>';
            $data[$i]['detials'] .= '出发日期：'.$data[$i]['date'].$data[$i]['stime'].'<br>';
            $data[$i]['detials'] .= empty($data[$i]['end_date'])?'':'返回日期：'.date('Y/m/d', $v['end_date']).$data[$i]['etime'].'<br>';
            $data[$i]['detials'] .= '出发地：'.$data[$i]['s_addr'];
            $data[$i]['detials'] .= '&nbsp&nbsp目的地：'.$data[$i]['e_addr'].'<br>';
            $data[$i]['detials'] .= empty($data[$i]['content'])?'':'其它信息：'.$data[$i]['content'];
            // $tmpObj = json_decode($data[$i]['content'],true);
            // $tmpObj = array('date'=>'2018-10-23','desc'=>'上午到上海');
            // var_dump(json_encode($tmpObj));
            // $data[$i]['content'] = '预定日期'.$tmpObj['date'].':'.$tmpObj['desc'];
            $confirmUrl = "javascript:if(confirm('确认接受该订单?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>2))."'";
            $rejectUrl = "javascript:refusePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>1))."')";
            $rejectCancelUrl = "javascript:refusePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>2))."')";
            $confirmCancelUrl = "javascript:if(confirm('确认取消该订单?'))location='".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>6))."'";
            $doneUrl = "javascript:donePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>4))."')";
;
            $tmpStrt = $data[$i]['comment'];
            $chaneCommentUrl = "javascript:refusePrompt('".U('changeOrderStatus', array('id'=> $v['id'], 'type'=>$type, 'status'=>$v['status']))."','".$tmpStrt."')";
            if($status == 0) {
                if($v['status'] == 0) {
                    $data[$i]['action'] = "<a href=$confirmUrl><font style='font-weight: bold' size='3'>确认</font></a>".'&nbsp&nbsp'."<a href=$rejectUrl><font style='font-weight: bold' size='3' color='red'>拒绝</font></a>";
                } else {
                    // $data[$i]['action'] = "<a href=$confirmCancelUrl><font style='font-weight: bold' size='3'>确认</font></a>".'&nbsp&nbsp'."<a href=$rejectCancelUrl><font style='font-weight: bold' size='3' color='red'>拒绝</font></a>";
                    $data[$i]['action'] = "<a href=$confirmCancelUrl><font style='font-weight: bold' size='3'>确认</font></a>";
                }

            } else if(($status == 1)|| ($status == 4)) {
                $data[$i]['action'] = "<a href=$chaneCommentUrl><font style='font-weight: bold' size='3'>修改备注</font></a>";
            } else if ($status == 2) {
                // $data[$i]['action'] = "<a href=$doneUrl><font style='font-weight: bold' size='3'>完成</font></a>".'&nbsp&nbsp'."<a class='confirm' href=$notcomeUrl><font style='font-weight: bold' size='3' color='red'>未到</font></a>";
                $url = addons_url('OaSystem://OaLogisticsService/ticketDone/id/'.$v['id']);
                $data[$i]['action'] = "<a class='confirm' href=$url><font style='font-weight: bold' size='3' color='red'>订单完成</font></a>";
            }
        }

        for($i = 0; $i < count($status_sel); $i++) {
            if($status_sel[$i]['id'] == $status) {
                $status_sel[$i]['selected'] ='selected';
            }
        }


        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }

        $this->assign('table_name',array('订单号','姓名','票类型','预定信息','订票结果','状态','操作'));
        $this->assign('_page',$list_data['_page']);
        $this->assign('status_sel', $status_sel);
        $this->assign('table_data', $data);
        $this->assign('type', $type);
        $this->assign('post_url', U('ticketOrderManage'));
        $this->display('ticketorder');
    }

    private function ticketOrderStatus() {
        $id = $_GET['id'];
        $status = $_GET['status'];
        $type = $_GET['type'];

        if(empty($id) || empty($status) || empty($type)) {
            $this->error(__METHOD__.'['.__LINE__.']'.'参数错误！');
            return false;
        }
        if(($order = db_find('oa_service_order', ['id'=>$id])) == NULL) {
            $this->error(__METHOD__.'['.__LINE__.']'.'找不到对应的订单！');
            return false;
        }
        if (!empty($_GET['comment'])) {
            $order['comment'] = $_GET['comment'];
        }
        $oldstatus = $order['status'];
        $order['status'] = $status;
        if(db_save('oa_service_order', ['id'=>$id], $order) === false) {
            $this->error(__METHOD__.'['.__LINE__.']'.'订单状态修改失败！');
            return false;
        }

        redirect(U('ticketOrderManage', array('status'=>$oldstatus)));
    }

    private function ticketTotelstatistics ($querypara) {

        $map['type'] = $querypara['type'];
        $s_time = strtotime($querypara['start_date']." 00:00:00");
        $e_time = strtotime($querypara['end_date']." 23:59:59");
        $map['time'] = array('between',array($s_time,$e_time));
        $map['is_del'] = 0;
        $allOrdersNum = M('OaServiceOrder')->where($map)->count();
        $map['status'] = 4;  //  预定完成
        $allDonesNum =  M('OaServiceOrder')->where($map)->count();
        $map['status'] = 6;  //  预定完成
        $allCancelNum =  M('OaServiceOrder')->where($map)->count();

        $data = array();
        $tmp['name'] = '预定完成百分比';
        $tmp['value'] = round((floatval($allDonesNum) / $allOrdersNum)*100).'%';
        $data[] = $tmp;
        $tmp['name'] = '预定取消百分比';
        $tmp['value'] = round((floatval($allCancelNum) / $allOrdersNum)*100).'%';
        $data[] = $tmp;
        $this->assign('total' ,$data);
    }
    private function ticketDonelist ($querypara) {
        $map['status'] = 4;  //  预定完成
        $map['type'] = $querypara['type'];
        $s_time = strtotime($querypara['start_date']." 00:00:00");
        $e_time = strtotime($querypara['end_date']." 23:59:59");
        $map['time'] = array('between',array($s_time,$e_time));
        $map['is_del'] = 0;

        $items = D('OaOrderServiceView')->where($map)->select();
        $data = array();
        foreach ($items as $item){
            $tmp = array();
            $tmp[] = $item['order_no'];
            $tmp[] = $item['emp_name'];
            $tmp[] = $item['name'];
            // $tmp[] = $item['num'];
            $tmp[] = $item['total'];
            $tmp[] = date('Y/m/d', $item['date']);
            array_push($data,$tmp);
        }

        $this->assign('table_name',array('订单号','姓名','票务类型','金额','时间'));
        $this->assign('table_data', $data);
    }

    function ticketStatistics() {
        if(IS_POST) {
            $querypara = json_encode($_POST);
            redirect(U('ticketStatistics', array('query'=>$querypara)));
        } else {
            $showTotal = false;
            $querypara = json_decode($_GET['query'], true);

            if (empty($querypara['start_date']) || empty($querypara['end_date'])) {
                $querypara['start_date'] = date("Y-m-d");
                $querypara['end_date'] = date("Y-m-d");
                $querypara['status'] = 0;
            }
            $querypara['type'] = 3;

            switch ($querypara['status']) {
                case 0 :
                    $this->serviceOderlist($querypara);
                    break;
                case 1:
                    $this->serviceOderConformlist($querypara);
                    break;
                case 2:
                    $this->serviceOderNotDonelist($querypara);
                    break;
                case 4:
                    $showTotal = true;
                    $this->ticketDonelist($querypara);
                    $this->ticketTotelstatistics($querypara);
                    break;
            }

            $status_sel = array(
                array('id'=>0,'name'=>'预订中统计'),array('id'=>4,'name'=>'销售统计'));
            $status = $querypara['status'];
            for($i = 0; $i < count($status_sel); $i++) {
                if($status_sel[$i]['id'] == $status) {
                    $status_sel[$i]['selected'] ='selected';
                }
            }

            $this->assign('showtotal',$showTotal?'':'hidden');
            $this->assign('data', $querypara);
            $this->assign('status_sel', $status_sel);
            $this->assign('query', $query);
            $this->assign('post_url', U('ticketStatistics'));
            $this->display('ticketstatistics');
        }
    }
    public function ticketDone () {
        if(IS_POST) {
            $id = $_GET['id'];
            $order = db_find('oa_service_order', ['id'=>$id]);
            $order['comment'] = $_POST['comment'];
            $order['total'] = $_POST['total'];
            $order['status'] = 4;
            M('OaServiceOrder')->save($order);
            $this->success('提交成功！', U('OaSystem/OaLogisticsService/ticketOrderManage/status/2'));
        }else{
            if(empty(I('id'))){
                $this->error ( '参数错误！！！');
            }

            $this->assign('post_url',addons_url('OaSystem://OaLogisticsService/ticketDone/id/'.I('id')));
            $this->display('ticketorder_done');
        }
    }
}